package com.example.webdownloadserver.handle.upload;

import com.example.webdownloadserver.base.TaskHandlerAbstract;
import com.example.webdownloadserver.base.TaskJobException;
import com.example.webdownloadserver.beans.UserExcel;
import com.example.webdownloadserver.constants.TaskConfigConstants;
import com.example.webdownloadserver.core.ITaskProcess;
import com.example.webdownloadserver.core.UploadHandler;
import com.example.webdownloadserver.handle.upload.thread.UploadUserThread;
import com.example.webdownloadserver.vo.TaskCenter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@UploadHandler(handler = TaskConfigConstants.USER_UPLOAD)
public class UploadUserHandler extends TaskHandlerAbstract implements ITaskProcess {
	
	private Logger logger = LoggerFactory.getLogger(UploadUserHandler.class);
	
	@Autowired
	UploadUserThread uploadUserThread;

	@Override
	public void handler(TaskCenter taskCenter) throws TaskJobException {
		try {
			//检查参数合法性
			checkUpload(taskCenter);
			//获取文件数据
			List<UserExcel> datalist = readExcelFile(taskCenter, UserExcel.class, 0);
			//线程处理
			uploadUserThread.doing(datalist, taskCenter);
		} catch (TaskJobException e) {
			throw e;
		} catch (Exception e) {
			logger.error("UploadUserHandler error",e);
			throw new TaskJobException("用户信息导入异常", e);
		} 	
	}

}
